﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JiaBao.Core.Plugins;

namespace JiaBao.Services.WPFFormPlug
{
    public class WPFFormService : IWPFFormService
    {
        #region 字段
        private readonly IPluginFinder _pluginFinder;
        #endregion

        #region Ctor
        public WPFFormService(IPluginFinder pluginFinder)
        {
            _pluginFinder = pluginFinder;
        }
        #endregion

        /// <summary>
        /// 获取所有 IWPFFormPlugin 插件
        /// </summary>
        /// <returns></returns>
        public IList<IWPFFormPlugin> LoadAllForms()
        {
            return _pluginFinder.GetPlugins<IWPFFormPlugin>( LoadPluginsMode.All).ToList();
        }


        /// <summary>
        /// 根据带单名获取插件
        /// </summary>
        /// <param name="displayNenuName"></param>
        /// <returns></returns>
        public IList<IWPFFormPlugin> LoadFormByDisplayMenuName(string displayNenuName)
        {
            return _pluginFinder.GetPlugins<IWPFFormPlugin>().Where(x => x.GetDisplayMenuName().Contains(displayNenuName, StringComparer.InvariantCultureIgnoreCase)).ToList();
        }

        /// <summary>
        /// 根据systemName获取插件
        /// </summary>
        /// <param name="systemName"></param>
        /// <returns></returns>
        public IWPFFormPlugin LoadFormBySystemName(string systemName)
        {
            var descriptor = _pluginFinder.GetPluginDescriptorBySystemName<IWPFFormPlugin>(systemName);
            if (descriptor != null)
                return descriptor.Instance<IWPFFormPlugin>();

            return null;
        }
    }
}
